/**hofit**/
package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JPasswordField;
import javax.swing.JToggleButton;
import java.awt.SystemColor;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import Controler.*;
import DAL.*;

import Client.BusinessImplements;
import Client.ServerProxy;
import Controler.*;
import DAL.SysUser_DAL;
import javax.swing.border.LineBorder;
public class Log_in_panel extends Abstract_Jpanel {
	private final JPanel panel = new JPanel();
	private final JLabel lblId = new JLabel("ID:");
	
	private final JLabel lblPASSWORD = new JLabel("PASSWORD:");
	private final JPasswordField passwordField = new JPasswordField();
	public  JButton accessToggleButton = new JButton("can't access your account?");
	public final JButton btnSignIn = new JButton("sign in");
	private final JLabel ipLable = new JLabel("IP:");
	private final JLabel lblport = new JLabel("port:");
	private final JTextField textFieldID = new JTextField();
	private final JTextField text_Fielip = new JTextField();
	private final JTextField port_text = new JTextField();
	public final JButton btnGuestSignIn = new JButton("guest sign in");

	/**
	 * Create the panel.
	 */
	public Log_in_panel() {
		port_text.setText("5555");
		port_text.setBounds(222, 222, 86, 20);
		port_text.setColumns(10);
		text_Fielip.setText("127.0.0.1");
		text_Fielip.setBounds(66, 223, 86, 20);
		text_Fielip.setColumns(10);
		textFieldID.setBounds(135, 28, 131, 20);
		textFieldID.setColumns(10);

		initGUI();
	}
	private void initGUI() {
		setForeground(new Color(0, 0, 0));
		setLayout(null);
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBounds(26, 11, 400, 201);
		panel.setBackground(SystemColor.inactiveCaption);
		
		add(panel);
		panel.setLayout(null);
		lblId.setForeground(new Color(0, 0, 0));
		lblId.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblId.setBounds(29, 28, 25, 17);
		
		panel.add(lblId);
		
		panel.add(textFieldID);
		lblPASSWORD.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblPASSWORD.setBounds(29, 56, 101, 28);
		
		panel.add(lblPASSWORD);
		passwordField.setBounds(135, 59, 131, 20);
		
		panel.add(passwordField);
		/**
		 * 
		 */
		accessToggleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ServerProxy s=new ServerProxy(getip(), getport());
				BusinessImplements.GetInstance().setServerProxy(s);
				Accesss_pass_panel nextscreen = new Accesss_pass_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(nextscreen);
				nextscreen.on_screen_load();
				nextscreen.setSize(getMaximumSize());
			}
		});
		accessToggleButton.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		accessToggleButton.setBackground(SystemColor.inactiveCaption);
		accessToggleButton.setForeground(Color.RED);
		accessToggleButton.setBounds(29, 97, 232, 23);
		panel.add(accessToggleButton);
		
		/**
		 *  being done when the user press sign in
		 *  call the function checkconnection that return true if the connection done
		 *  and false if not
		 *  if the connection don't done: inform the user that the user name or password incorrect
		 *  if the connection done: check if the user already connect 
		 *  if he not connect: connect the user to the system
		 *  if the user already connect: inform the user
		 */
		btnSignIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Log_in_panel_controler con_log= new Log_in_panel_controler();
				on_screen_unload();
				if(con_log.checkconnection(getid(), getip(), getport(), getpassword()))
				{
					if (con_log.checkonline(getid()).get(0).equals("0"))
					{
						Sign_in_panel nextscreen = new Sign_in_panel();
						SysUser_DAL userLogin=con_log.getUserById(getid());
						BusinessImplements.GetInstance().setUserLogin(userLogin);
						con_log.update_useronline(getid());
						BusinessImplements.GetInstance().getMainFrame().set_max_size();
						BusinessImplements.GetInstance().getMainFrame().setContentPane(nextscreen);
						nextscreen.setSize(getMaximumSize());
						nextscreen.on_screen_load();
					}
					else
						JOptionPane.showMessageDialog(null,"User already connect!");
				}
				else
					JOptionPane.showMessageDialog(null,"User name or password incorrect!");
			}
		});
		btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnSignIn.setBounds(269, 142, 101, 36);
		
		panel.add(btnSignIn);
		ipLable.setFont(new Font("Tahoma", Font.PLAIN, 14));
		ipLable.setBounds(36, 221, 28, 20);
		
		add(ipLable);
		lblport.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblport.setBounds(181, 223, 46, 14);
		
		add(lblport);
		
		add(text_Fielip);
		
		add(port_text);
		/**
		 * connect to the system and allow only search
		 */
		btnGuestSignIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Log_in_panel_controler con_log= new Log_in_panel_controler();
				if(con_log.createServerProxyForGuest(getip(), getport()))
				{
				Sign_in_panel nextscreen = new Sign_in_panel();
				SysUser_DAL userLogin=new SysUser_DAL(0," "," ",0);
				BusinessImplements.GetInstance().setUserLogin(userLogin);
				BusinessImplements.GetInstance().getMainFrame().set_max_size();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(nextscreen);
				nextscreen.setSize(getMaximumSize());
				nextscreen.on_screen_load();
				}
			}
		});
		
		btnGuestSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGuestSignIn.setBounds(279, 253, 125, 36);
		
		add(btnGuestSignIn);
	}
	
	
	
	public String getid ()
	{
		return this.textFieldID.getText();
	}
	public String getip ()
	{
		return this.text_Fielip.getText();
	}
	public String getport ()
	{
		return this.port_text.getText();
	}
	
	public String getpassword ()
	{
		return this.passwordField.getText();
	}
	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_load() {
		//JOptionPane.showMessageDialog(this,"Roseindia.net");
		
	}
	@Override
	public void on_screen_unload() {
		//JOptionPane.showMessageDialog(this,"Roseindia.net");
		
	}
	
	
	
	
}
